package com.liutao.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.liutao.*;


public class LiuTaoServlet extends HttpServlet {

	int V=0;
	int C=0;
	double S=-1.0;
	/**
	 * Constructor of the object.
	 */
	public LiuTaoServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String baseFilePath = getServletContext().getRealPath("/") ;
		System.out.println("baseFilePath:" + baseFilePath);
		small_world sm=new small_world();
		String flag="";
		 flag=request.getParameter("flag");
		if(flag.equals("lt")){
			String strV = request.getParameter("nodes");
            String strC = request.getParameter("degree");
			//String strS=request.getParameter("threshold");
			if(!strV.isEmpty())
				V=Integer.parseInt(strV);
			if(!strC.isEmpty())
				C=Integer.parseInt(strC);
			//if(!strS.isEmpty())
				//S=Double.parseDouble(strS);
			System.out.println("creat_net"+V+" "+C);
			if(V>0){
				sm.setV(V);
			}
			if(C>0){
				sm.setC(C);
			}
		//	if(S>0.00)
			//	sm.setS(S);
			sm.gen(baseFilePath);
		     	JSONArray jn=new JSONArray();
			    response.setContentType("application/x-json");
	            response.setCharacterEncoding("UTF-8");
		        PrintWriter out = response.getWriter();
		        out.print(jn);
		        out.flush();
	        	out.close();
		}
		else
		{
			String strS=request.getParameter("threshold");
			if(!strS.isEmpty())
				S=Double.parseDouble(strS);
			
			if(V>0){
				sm.setV(V);
			}
			if(C>0){
				sm.setC(C);
			}
			if(S>0.00)
				sm.setS(S);
			
			   JSONArray jn=new JSONArray();
		       int[][] edge=sm.readText(baseFilePath);
		        jn=sm.diffusion(edge);	   
		        response.setContentType("application/x-json");
	            response.setCharacterEncoding("UTF-8");
		        PrintWriter out = response.getWriter();
		        out.print(jn);
		        out.flush();
	        	out.close();
			
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
